/*
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.action;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.resources.RemoteFile;
import net.sourceforge.perforce.ui.internal.Policy;
import net.sourceforge.perforce.ui.views.HistoryView;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;

/**
  * ObjectContribution for net.sourceforge.perforce.core.resources.RemoteFile
  *  
  * @version $Revision: 21 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ShowHistoryAction extends PerforceAction
{
	// ------------------------------------------------------------------------------ Public Methods

	/** @see org.eclipse.ui.IActionDelegate#run(IAction) */
	public void run(IAction action) 
	{
		run(new IRunnableWithProgress() {
			/** @see org.eclipse.jface.operation.IRunnableWithProgress#run(IProgressMonitor) */
			public void run(IProgressMonitor monitor)
				throws InvocationTargetException, InterruptedException 
			{
				RemoteFile[] files = getSelectedRemoteFiles();
				Policy.assertTrue(files.length == 1);
				
				HistoryView view = HistoryView.openInActivePerspective();
				if (view != null)
				{
					view.showHistory(files[0]);
				}
			}
		}, PROGRESS_BUSYCURSOR);
	}

	// --------------------------------------------------------------------------- Protected Methods

	/** @see net.sourceforge.perforce.ui.action.PerforceAction#isEnabled() */
	protected boolean isEnabled() 
		throws PerforceException 
	{
		RemoteFile[] resources = getSelectedRemoteFiles();
		return resources.length == 1;
	}
}
